package com.manager.system.controller;

import com.manager.common.annotation.AspectLogInfo;
import com.manager.common.constant.AspectMethodOperationEnum;
import com.manager.common.constant.OperationLogEnumType;
import com.manager.common.office.sax.ExcelFactory;
import com.manager.common.pdf.File2PdfUtils;
import com.manager.common.token.TokenManager;
import com.manager.common.utils.LargeFileHttpUploadUtils;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.File_Entity;
import com.manager.model.Operation_Log;
import com.manager.system.service.FileService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JsonConfig;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 文件管理
 * @author zlj
 *
 */
@RestController
@RequestMapping("/file")
@SuppressWarnings("all")
public class FileController  {

	@Autowired
	private FileService fileServiceImpl;
	
	@RequestMapping("/getFilePage")
	public PageVO getFilePage(File_Entity model, HttpServletRequest request, HttpServletResponse response) throws IOException {
		return fileServiceImpl.getFilePage(model);
	}

	@RequestMapping("/convertFile")
	public ResultVO converFile(File_Entity model, HttpServletResponse response, HttpServletRequest request) throws Exception{
		try{
			File_Entity fileEntity = fileServiceImpl.getFileEntityById(model.getId());
			if(StringUtils.isNotEmpty(fileEntity.getConvertFilePath())){
				return ResultVO.success("",fileEntity.getConvertFilePath());
			}
			if(fileEntity.getFilePath().toLowerCase().endsWith("pdf")){
				return ResultVO.success("",fileEntity.getFilePath());
			}
			String fileRealPath = LargeFileHttpUploadUtils.getBasePath() + "/" + fileEntity.getFilePath();
			if(ObjectUtils.isImage(new File(fileRealPath))){
				return ResultVO.success("",fileEntity.getFilePath());
			}
			if(!File2PdfUtils.canConvert(fileEntity.getFilePath())){
				return ResultVO.failed("支持预览的文件【"+File2PdfUtils.getSuffix()+"】");
			}
			String  filePath = LargeFileHttpUploadUtils.getBasePath()  + fileEntity.getFilePath();
			String converFilePath = File2PdfUtils.file2Pdf(filePath);
			converFilePath = converFilePath
					.substring(LargeFileHttpUploadUtils.getBasePath().length(), converFilePath.length())
					.replaceAll("\\\\", "/");
			fileEntity.setConvertFilePath(converFilePath);
			fileServiceImpl.updateFileConvertFilePath(fileEntity);
			return ResultVO.success("",converFilePath);
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResultVO.failed("文件转换异常!");
	}

	/**
	 * 导出日志Excel数据
	 * @param model
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/dowloadFile")
	public void exportExcelDatas(File_Entity model, HttpServletRequest request, HttpServletResponse response) throws IOException{

		try{
			File_Entity fileEntity = fileServiceImpl.getFileEntityById(model.getId());
			File file = new File(LargeFileHttpUploadUtils.getBasePath() + "/" + fileEntity.getFilePath());
			if(!file.exists()){
				return;
			}
			response.setContentType("application/force-download");
			response.addHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode("判决日志", "UTF-8")+"." + fileEntity.getFileSuffix() );

			byte[] buffer = new byte[1024];
			FileInputStream fis = null;
			BufferedInputStream bis = null;
			try {
				fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				OutputStream os = response.getOutputStream();
				int i = bis.read(buffer);
				while (i != -1) {
					os.write(buffer, 0, i);
					i = bis.read(buffer);
				}
			} finally {
				if (bis != null) {
					bis.close();
				}
				if (fis != null) {
					fis.close();
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	@RequestMapping("/getDownloadToken")
	public ResultVO getDownloadToken(){
		try {
			String token = TokenManager.getInstance().generateDownloadToken();
			return ResultVO.success("",token);
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
}
